﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;

    public class ManPowerResourceDA : IManPowerResourceDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string EQUIPMENT_PROFILE_ID = "@EQUIPMENT_PROFILE_ID";
        private string FORKLIFT_DRIVER = "@FORKLIFT_DRIVER";
        private string IS_HOLIDAY = "@IS_HOLIDAY";
        private string ORDER_ID = "@ORDER_ID";
        private string ORDER_TYPE = "@ORDER_TYPE";
        private string REMARK = "@REMARK";
        private string SQL_CHECK_MAN_POWER_RESOURCE_ID_UNIQUE = " SELECT COUNT(1) FROM @_@MAN_POWER_RESOURCE WHERE ORDER_ID = @ORDER_ID AND ORDER_TYPE = @ORDER_TYPE ";
        private string SQL_DELETE_MAN_POWER_RESOURCE = " DELETE FROM @_@MAN_POWER_RESOURCE WHERE ORDER_ID = @ORDER_ID AND ORDER_TYPE = @ORDER_TYPE ";
        private string SQL_INSERT_MAN_POWER_RESOURCE = "INSERT INTO @_@MAN_POWER_RESOURCE(WH_ID,ORDER_ID,ORDER_TYPE,STORE_MAN,FORKLIFT_DRIVER,WORK_TYPE,WORK_CLASS,EQUIPMENT_PROFILE_ID,IS_HOLIDAY,REMARK,CREATED_BY,CREATED_DATE,UPDATED_BY,UPDATED_DATE)VALUES(@WH_ID,@ORDER_ID,@ORDER_TYPE,@STORE_MAN,@FORKLIFT_DRIVER,@WORK_TYPE,@WORK_CLASS,@EQUIPMENT_PROFILE_ID,@IS_HOLIDAY,@REMARK,@CREATED_BY,GETDATE(),@UPDATED_BY,GETDATE())";
        private string SQL_SELECT_ALL_MAN_POWER_RESOURCE = "SELECT MPR.ROWID,MPR.WH_ID,MPR.ORDER_ID,MPR.ORDER_TYPE,MPR.STORE_MAN,MPR.FORKLIFT_DRIVER,MPR.WORK_TYPE,MPR.WORK_CLASS,MPR.EQUIPMENT_PROFILE_ID,MPR.IS_HOLIDAY,MPR.REMARK,MPR.TS,MPR.CREATED_BY,MPR.CREATED_DATE,MPR.UPDATED_BY,MPR.UPDATED_DATE FROM @_@MAN_POWER_RESOURCE MPR";
        private string SQL_UPDATE_MAN_POWER_RESOURCE = " UPDATE @_@MAN_POWER_RESOURCE SET WH_ID = @WH_ID,STORE_MAN = @STORE_MAN,FORKLIFT_DRIVER = @FORKLIFT_DRIVER,WORK_TYPE = @WORK_TYPE,WORK_CLASS = @WORK_CLASS,EQUIPMENT_PROFILE_ID = @EQUIPMENT_PROFILE_ID, IS_HOLIDAY = @IS_HOLIDAY,REMARK = @REMARK,UPDATED_BY = @UPDATED_BY,UPDATED_DATE = GETDATE() WHERE ORDER_ID = @ORDER_ID AND ORDER_TYPE = @ORDER_TYPE";
        private string STORE_MAN = "@STORE_MAN";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";
        private string WORK_CLASS = "@WORK_CLASS";
        private string WORK_TYPE = "@WORK_TYPE";

        public bool CheckManPowerResourceUnique(string orderID, string orderType, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ORDER_ID", orderID), new SqlParameter("@ORDER_TYPE", orderType) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_MAN_POWER_RESOURCE_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckManPowerResourceUnique(DataBase dataBase, DbTransaction tran, string orderID, string orderType, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ORDER_ID", orderID), new SqlParameter("@ORDER_TYPE", orderType) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_MAN_POWER_RESOURCE_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteManPowerResource(List<string> orderID, List<string> orderType, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < orderID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ORDER_ID", orderID[j]), new SqlParameter("@ORDER_TYPE", orderType[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_MAN_POWER_RESOURCE.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteManPowerResource(DataBase dataBase, DbTransaction tran, List<string> orderID, List<string> orderType, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int j = 0; j < orderID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ORDER_ID", orderID[j]), new SqlParameter("@ORDER_TYPE", orderType[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_MAN_POWER_RESOURCE.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllManPowerResource(string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_MAN_POWER_RESOURCE.Replace("@_@", whLoginID);
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllManPowerResource(DataBase dataBase, DbTransaction tran, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_MAN_POWER_RESOURCE.Replace("@_@", whLoginID);
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public ManPowerResourceInfo GetManPowerResourceByID(string orderID, string orderType, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_MAN_POWER_RESOURCE.Replace("@_@", whLoginID) + " WHERE MPR.ORDER_ID = @ORDER_ID AND MPR.ORDER_TYPE = @ORDER_TYPE ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ORDER_ID", orderID), new SqlParameter("@ORDER_TYPE", orderType), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            ManPowerResourceInfo manpowerResourceInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    manpowerResourceInfo = new ManPowerResourceInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["ORDER_ID"].ToString(), reader["ORDER_TYPE"].ToString(), reader["STORE_MAN"].ToString(), reader["FORKLIFT_DRIVER"].ToString(), reader["WORK_TYPE"].ToString(), reader["WORK_CLASS"].ToString(), reader["EQUIPMENT_PROFILE_ID"].ToString(), reader["IS_HOLIDAY"].ToString(), reader["REMARK"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return manpowerResourceInfo;
        }

        public ManPowerResourceInfo GetManPowerResourceByID(DataBase dataBase, DbTransaction tran, string orderID, string orderType, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_MAN_POWER_RESOURCE.Replace("@_@", whLoginID) + " WHERE MPR.ORDER_ID = @ORDER_ID AND MPR.ORDER_TYPE = @ORDER_TYPE ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ORDER_ID", orderID), new SqlParameter("@ORDER_TYPE", orderType), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            ManPowerResourceInfo manpowerResourceInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                manpowerResourceInfo = new ManPowerResourceInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["ORDER_ID"].ToString(), reader["ORDER_TYPE"].ToString(), reader["STORE_MAN"].ToString(), reader["FORKLIFT_DRIVER"].ToString(), reader["WORK_TYPE"].ToString(), reader["WORK_CLASS"].ToString(), reader["EQUIPMENT_PROFILE_ID"].ToString(), reader["IS_HOLIDAY"].ToString(), reader["REMARK"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return manpowerResourceInfo;
        }

        public int InsertManPowerResource(ManPowerResourceInfo manpowerResourceInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                manpowerResourceInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_ManPowerResource_Parameters(manpowerResourceInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_MAN_POWER_RESOURCE.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertManPowerResource(DataBase dataBase, DbTransaction tran, ManPowerResourceInfo manpowerResourceInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                manpowerResourceInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_ManPowerResource_Parameters(manpowerResourceInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_MAN_POWER_RESOURCE.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_ManPowerResource_Parameters(ManPowerResourceInfo manpowerResourceInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.ORDER_ID, SqlDbType.VarChar, 30), new SqlParameter(this.ORDER_TYPE, SqlDbType.VarChar, 10), new SqlParameter(this.STORE_MAN, SqlDbType.VarChar, 30), new SqlParameter(this.FORKLIFT_DRIVER, SqlDbType.VarChar, 30), new SqlParameter(this.WORK_TYPE, SqlDbType.VarChar, 20), new SqlParameter(this.WORK_CLASS, SqlDbType.VarChar, 20), new SqlParameter(this.EQUIPMENT_PROFILE_ID, SqlDbType.VarChar, 30), new SqlParameter(this.IS_HOLIDAY, SqlDbType.VarChar, 1), new SqlParameter(this.REMARK, SqlDbType.VarChar, 300), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 30), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 30) };
            if (!string.IsNullOrEmpty(manpowerResourceInfo.WhID))
            {
                paramArray[0].Value = manpowerResourceInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = manpowerResourceInfo.OrderID;
            paramArray[2].Value = manpowerResourceInfo.OrderType;
            if (!string.IsNullOrEmpty(manpowerResourceInfo.StoreMan))
            {
                paramArray[3].Value = manpowerResourceInfo.StoreMan;
            }
            else
            {
                paramArray[3].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(manpowerResourceInfo.ForkliftDriver))
            {
                paramArray[4].Value = manpowerResourceInfo.ForkliftDriver;
            }
            else
            {
                paramArray[4].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(manpowerResourceInfo.WorkType))
            {
                paramArray[5].Value = manpowerResourceInfo.WorkType;
            }
            else
            {
                paramArray[5].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(manpowerResourceInfo.WorkClass))
            {
                paramArray[6].Value = manpowerResourceInfo.WorkClass;
            }
            else
            {
                paramArray[6].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(manpowerResourceInfo.EquipmentProfileID))
            {
                paramArray[7].Value = manpowerResourceInfo.EquipmentProfileID;
            }
            else
            {
                paramArray[7].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(manpowerResourceInfo.IsHoliday))
            {
                paramArray[8].Value = manpowerResourceInfo.IsHoliday;
            }
            else
            {
                paramArray[8].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(manpowerResourceInfo.Remark))
            {
                paramArray[9].Value = manpowerResourceInfo.Remark;
            }
            else
            {
                paramArray[9].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(manpowerResourceInfo.CreatedBy))
            {
                paramArray[10].Value = manpowerResourceInfo.CreatedBy;
            }
            else
            {
                paramArray[10].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(manpowerResourceInfo.UpdatedBy))
            {
                paramArray[11].Value = manpowerResourceInfo.UpdatedBy;
                return paramArray;
            }
            paramArray[11].Value = DBNull.Value;
            return paramArray;
        }

        public int UpdateManPowerResource(ManPowerResourceInfo manpowerResourceInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                manpowerResourceInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_ManPowerResource_Parameters(manpowerResourceInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_MAN_POWER_RESOURCE.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateManPowerResource(DataBase dataBase, DbTransaction tran, ManPowerResourceInfo manpowerResourceInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                manpowerResourceInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_ManPowerResource_Parameters(manpowerResourceInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_MAN_POWER_RESOURCE.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

